package com.woniu.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;

public class JwtUtil {

//	private static final String adminSecret = "askdfjLKASJDFLASJFaslkdfjalksjdf13123lksdfjlkasfasdfasdfadf";
//
//	private static final String userSecret = "askdfjLKASJDFLaksdfdksaslkdfjalksjdf131123iwuerdfjlkasfasdfasdfadf";

	private static final String secret = "sjnfjanfnsfjninignrineangeijniengineinenjfnegnejifnijedfifisnfeij";
	
	/**
	 * 生成Token
	 * @param body
	 * @param time 秒
	 * @return
	 */
	public static String createToken(Map<String, Object> body,Integer time) {
		SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
		JwtBuilder jb = Jwts.builder();
		if(body == null || body.size() <= 0) throw new RuntimeException("没有数据");
		jb.setClaims(body);
		if(time == null) jb.setExpiration(new Date(System.currentTimeMillis() + 60 * 300 * 1000));
		else jb.setExpiration(new Date(System.currentTimeMillis() + time * 1000));
		return jb.signWith(key).compact();
	}



	
	/**
	 * 解析
	 * @param token
	 * @return
	 */
	public static Claims parseToken(String token) throws Exception {
		if(token == null) throw new RuntimeException("token为空");
		SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
		return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
	}
	


//	/**
//	 * 解析User端token
//	 * @param token
//	 * @return
//	 */
//	public static Claims parseUserToken(String token) throws Exception {
//		return parseToken(token,userSecret);
//	}
//
//	/**
//	 * 解析Admin端token
//	 * @param token
//	 * @return
//	 */
//	public static Claims parseAdminToken(String token) throws Exception {
//		return parseToken(token,adminSecret);
//	}

}







